package nl.saxion.act.dao.generic;

import java.lang.reflect.ParameterizedType;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public abstract class AbstractDao<K, E> implements IDao<K, E> {
	protected Class<E> entityClass;
	
	@PersistenceContext
	protected EntityManager entityManager;
	
	@SuppressWarnings( "unchecked" )
	public AbstractDao() {
		ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
		this.entityClass = (Class<E>)genericSuperclass.getActualTypeArguments()[1];
	}
	
	public void persist(E entity) { 
		entityManager.persist(entity); 
	}

	public void remove(E entity) { 
		entityManager.remove(entity); 
	}

	public E findById(K id) { 
		return entityManager.find(entityClass, id);
	}
}
